#!/usr/bin/env/python3.7
# -*- coding: utf-8 -*-

"""
@File    : txt2mysql_demo.py
@Time    : 12/07/2021
@Author  : Silence
@Version : 1.0
@Contact : silence2021silence@163.com
@License : (C)Copyright 2021-2021, Silence
@Desc    : None
"""
import time
import pymysql

database = pymysql.connect(host="120.24.51.156", user="dormitory_system", password="123456",
                                   database="dormitory_system", charset="utf8")

class Main():
    def insert():
        t = True
        while t:
            with open('./student_info.txt', 'r') as f:
                line = f.readlines()
                student_number = line[0][15:].strip('\n')
                name = line[1][5:].strip('\n')
                room = line[2][5:].strip('\n')
                grade = line[3][6:].strip('\n')
                class_ = line[4][6:].strip('\n')
            (infrared_ttl, temperature_value, weight_value, volume_value, smoke_ttl, smoke_value) \
                = read_arduino.ReadArduino.read_arduino()
            cursor = database.cursor()
            sql_1 = """
                    INSERT INTO `dormitory_system`.`source_data` (
	                    `id`, `student_number`, `name`, `room`,
	                    `grade`, `class`, `upload_datetime`,
	                    `infrared_ttl`, `temperature_value`,
	                    `weight_value`, `volume_value`,
	                    `smoke_ttl`, `smoke_value`, `ip_address`
                        )
                    VALUES
	                    (
		                    NULL, '%s', '%s', '%s', '%s', '%s', '%s', 
		                    '%s', '%s', '%s', '%s', '%s', '%s', '%s'
	                    )
            """

            sql_2 = sql_1 % (student_number, name, room, grade, class_, infrared_ttl,
                             temperature_value, weight_value, volume_value, smoke_ttl, smoke_value, ipaddress)
            cursor.execute(sql_2)
            result = cursor.fetchall()
            print(sql_2)
            t = False
